<template>
  <div>
    <div class="bread">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '#' }" style="font-size: 20px;"
          >首页</el-breadcrumb-item
        >
        <el-breadcrumb-item style="font-size: 20px;"
          >人员管理</el-breadcrumb-item
        >
        <el-breadcrumb-item style="font-size: 20px;"
          >展示管理员</el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>

    <el-row style="margin-top: 20px;margin-left: 50px;">
      <el-col :span="5" :offset="0">
        <el-input placeholder="请输入内容" v-model="searchCondition">
          <template slot="prepend">昵称/电话:</template>
        </el-input>
      </el-col>
      <el-col :span="1">
        <el-button type="primary" @click="listAccounts()">搜索</el-button>
      </el-col>
    </el-row>
    <!-- <el-row>
      <el-col :span="1.5">
        <div style="margin-top: 20px; margin-bottom: 20px;"> -->
    <!-- <el-button type="success" @click="showAddWindow=true;" round>添加</el-button> -->
    <!-- <el-button type="danger" @click="delByChecked" round>删除</el-button>
        </div>
      </el-col>
    </el-row> -->

    <div ref="table">
      <el-table
        :data="page.list"
        border
        style="width: 1300px;margin-left: 50px;margin-top: 20px;"
        :stripe="true"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <!-- <el-table-column fixed prop="id" type="selection" label="楼宇id" width="150">
        </el-table-column> -->
        <el-table-column prop="accountName" label="账号名" width="200">
        </el-table-column>
        <el-table-column prop="accountNickname" label="昵称" width="200">
        </el-table-column>
        <el-table-column prop="accountPhone" label="电话" width="320">
        </el-table-column>
        <!-- <el-table-column prop="accountState" label="账号状态" width="320">
        </el-table-column> -->
        <el-table-column label="账号状态" width="260">
          <template slot-scope="scope">
            <span v-if="scope.row.accountState == 0"
              >正常<i style="color: limegreen;" class="el-icon-success"></i
            ></span>
            <span v-if="scope.row.accountState == 1"
              >已删除<i style="color: red;" class="el-icon-delete-solid"></i
            ></span>
            <span v-if="scope.row.accountState == 2"
              >已禁用<i style="color:chocolate;" class="el-icon-error"></i
            ></span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
            v-if="scope.row.accountState != 1"
              type="primary"
              icon="el-icon-edit"
              @click="handleClick(scope.row)"
              size="small"
              circle
              >修改</el-button
            >
            <el-button
              v-if="scope.row.accountState == 0"
              @click="disable(scope.row.accountId)"
              type="warning"
              icon="el-icon-error"
              size="mini"
              round
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.accountState == 2"
              @click="enable(scope.row.accountId)"
              type="success"
              size="mini"
              round
              >解禁</el-button
            >
            <el-button
              v-if="scope.row.accountState != 1"
              @click="delById(scope.row.accountId)"
              type="danger"
              icon="el-icon-delete-solid"
              round
              size="mini"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.accountState == 1"
              @click="undelete(scope.row.accountId)"
              type="primary"
              icon="el-icon-refresh-left"
              round
              size="mini"
              >撤回</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-row style="margin-left: 50px;">
      <el-col :span="5">
        <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageNumChange"
          :current-page="page.current"
          :page-sizes="[1, 5, 10]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
        >
        </el-pagination>
      </el-col>
    </el-row>

    <el-dialog :visible.sync="showUpdateWindow" width="30%">
      <template slot="title">
        <div style="font-size:18px;font-weight: 999;">修改账号信息</div>
      </template>
      <div style="width: 400px;">
        <el-form :model="account" label-width="100px" class="demo-ruleForm">
          <el-form-item label="昵称" prop="accountNickname">
            <el-input v-model="account.accountNickname"></el-input>
          </el-form-item>
          <el-button type="primary" @click="updateAccount">立即修改</el-button>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ShowAccount",
  data() {
    return {
      page: {},
      searchCondition: "",
      showUpdateWindow: false,
      account: {}
    };
  },
  methods: {
    pageSizeChange(size) {
      this.page.current = 1;
      this.splitePage(this.page.current, size);
    },
    pageNumChange(current) {
      this.splitePage(current, this.page.size);
    },
    listAccounts() {
      //查找全部
      //调用分页函数
      this.splitePage(this.page.current, this.page.size);
    },
    disable(id) {
      this.$http
        .get("/account/admin/disableAccount?accountId=" + id)
        .then(resp => {
          alert(resp.data.message);
          this.listAccounts();
        });
    },
    enable(id) {
      this.$http
        .get("/account/admin/enableAccount?accountId=" + id)
        .then(resp => {
          alert(resp.data.message);
          this.listAccounts();
        });
    },
    delById(id) {
      this.$http
        .get("/account/admin/deleteAccount?accountId=" + id)
        .then(resp => {
          alert(resp.data.message);
          this.listAccounts();
        });
    },
    splitePage(pageNum, pageSize) {
      //分页函数
      this.$http
        .get("/account/admin/searchAdmin", {
          //axios发送get请求 多个参数需要加params
          params: {
            condition: this.searchCondition,
            pageNum: pageNum,
            pageSize: pageSize
          }
        })
        .then(resp => {
          //将分页信息传入data区中
          this.page = resp.data.data;
        });
    },
    handleClick(obj) {
      this.account = JSON.parse(JSON.stringify(obj));
      this.showUpdateWindow = true;
    },
    updateAccount() {
      this.$http.post("/account/admin/updateAdmin", this.account).then(resp => {
        alert(resp.data.message);
        this.listAccounts();
        this.showUpdateWindow = false;
      });
    },
    undelete(id) {
      this.$http
        .get("/account/admin/undeleteAccount?accountId=" + id)
        .then(resp => {
          alert(resp.data.message);
          this.listAccounts();
        });
    }
  },
  created() {
    this.listAccounts();
  }
};
</script>

<style scoped>
.bread {
  margin-top: 50px;
  margin-left: 50px;
}
</style>

<style>
.el-input__inner::placeholder {
  color: #31e5f5;
}
</style>
